package cloud.keveri.gateway.plugin.base;

import cloud.keveri.gateway.plugin.api.KeveriPlugin;
import cloud.keveri.gateway.plugin.api.KeveriPluginChain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Objects;

import static cloud.keveri.gateway.plugin.base.def.Const.ERROR_FLAG;
import static cloud.keveri.gateway.plugin.base.def.Const.UNIQUE_ID;

/**
 * 基底
 * <p>
 * 作者：沈自在
 **/
@Slf4j
public abstract class AbstractKeveriPlugin implements KeveriPlugin {
    /**
     * 真实插件执行
     */
    protected abstract Mono<Void> doExecute(ServerWebExchange exchange, KeveriPluginChain chain);

    /**
     *
     */
    @Override
    public Mono<Void> process(ServerWebExchange exchange, KeveriPluginChain chain) {
        // 判断是否已经出错 (有错直接快速失败)
        Object errorFlag = exchange.getAttribute(ERROR_FLAG);
        if (Objects.nonNull(errorFlag)) return chain.execute(exchange);

        return doExecute(exchange, chain);
    }
}
